import axios from 'axios';
import store from '@/store/index';

// 创建axio实例 用于二次封装
const _axios = axios.create({
	timeout: 2000,
});

// 请求拦截器
_axios.interceptors.request.use(
	(config) => {
		// 判断接口是否为菜单接口，如果为菜单接口则指定接口URL
		if (config.url == '/showapi_cpType' || config.url == '/showapi_cpQuery') {
			config.headers['Authorization'] = 'APPCODE f3dcd3b062d7468db9c0d9b7a1ef512f';
			config.baseURL = 'http://caipu.market.alicloudapi.com';
		}

		store.commit('loading', true);
		return config;
	},
	(error) => {
		Promise.reject(error);
	}
);

// 响应拦截器
_axios.interceptors.response.use(
	(res) => {
		store.commit('loading', false);
		return res;
	},
	(err) => {
		return Promise.reject(err);
	}
);

export default _axios;
